<template>
	<view>
		<!-- 地址 -->
		<navigator url="/pages/address/address?source=createorder" class="address-section">
			<view class="order-content">
				<text class="yticon icon-shouhuodizhi"></text>
				<view class="cen" v-if="addressData.name !=''">
					<view class="top">
						<text class="name">{{addressData.name}}</text>
						<text class="mobile">{{addressData.mobile}}</text>
					</view>
					<text class="address">{{addressData.area_text}} {{addressData.address}}</text>
				</view>
				<view class="cen" v-else>
					<text>请选择地址</text>
				</view>
				<text class="yticon icon-you"></text>
			</view>

		 
		</navigator>

		<view class="goods-section">
			<view class="g-header b-b">
				<text class="name">商品列表</text>
			</view>
			<!-- 商品列表 -->

			<view class="g-item" v-for="(item,index) in product_list" :key="index">
				<image :src="item.cover_image"></image>
				<view class="right">
					<text class="title clamp">{{item.name}}</text>
					<text class="spec">{{item.spec_code_text ?item.spec_code_text.join(" ") : ""}}</text>
					<view class="price-box">
						<text class="price">￥{{item.show_price}}</text>
						<text class="number">x {{item.product_num}}</text>
					</view>
				</view>
			</view>
		</view>

		<!-- 优惠明细 -->
		<view class="yt-list">
			<view class="yt-list-cell b-b" @click="toggleMask('show')">
				<view class="cell-icon">
					券
				</view>
				<text class="cell-tit clamp">优惠券</text>
				<text class="cell-tip active"  >
					{{couponList.length}}张可用/已选{{usedcoupon.length}}张
				</text>
				<text class="cell-more wanjia wanjia-gengduo-d"></text>
			</view>

		</view>
		<!-- 金额明细 -->
		<view class="yt-list">
			<view class="yt-list-cell b-b">
				<text class="cell-tit clamp">商品金额</text>
				<text class="cell-tip">￥{{total_price}}</text>
			</view>
			<view class="yt-list-cell b-b">
							<text class="cell-tit clamp">优惠金额</text>
							<text class="cell-tip red">-￥{{coupon_plusamount}}</text>
						</view>
			<view class="yt-list-cell b-b">
				<text class="cell-tit clamp">运费</text>
				<text class="cell-tip">{{express_price}}</text>
			</view>
			<view class="yt-list-cell desc-cell">
				<text class="cell-tit clamp">备注</text>
				<input class="desc" type="text" v-model="remark" placeholder="请填写备注信息" placeholder-class="placeholder" />
			</view>
		</view>

		<!-- 底部 -->
		<view class="footer">
			<view class="price-content">
				<text>实付款</text>
				<text class="price-tip">￥</text>
				<text class="price">{{payprice}}</text>
			</view>
			<text class="submit" @click="submit">提交订单</text>
		</view>

		<!-- 优惠券面板 -->
		<view class="mask" :class="maskState===0 ? 'none' : maskState===1 ? 'show' : ''" @click="toggleMask">
			<view class="mask-content" @click.stop.prevent="stopPrevent">
				<!-- 优惠券页面，仿mt -->
				<checkbox-group @change="selectcoupon">
				<view class="coupon-item"  v-for="(item,index) in couponList" :key="index">
					
					<view class="item-left">
						<text>¥{{item.coupon_amount}}</text>
						<text class="tip">满{{item.limit_minprice}}可用</text>
					</view>
					<view class="item-right">
						<text>{{item.rule_descript}}</text>
						<view class="desc">
							<text>有效期至:{{item.end_usetime}}</text>
						 
								<checkbox class="getbtn" style="border-color: #FA436A;" color="#ffa200" :value="item.id"></checkbox>
							 
						</view>
					</view>
					
				</view>
				</checkbox-group>
			</view>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				maskState: 0, //优惠券面板显示状态
				remark: '', //备注
				couponList: [],
				payprice:0,
				usedcoupon:[],//选中的优惠劵
				addressData: {
					name:"",
					mobile:"",
					area_text:"",
					address:""
				},
				express_price: 0, //快递费
				total_price: 0, //总价,优惠前
				
				coupon_plusamount:0,//优惠券优惠了多少
				
			 
				product_list: [],
				commonbuyparam: {
					"product_id":0,
					"product_num":1,
					"spec_code":""
				},
				pintuanparam: { //拼团参数
					buy_type: "notuan",
					product_id: 0,
					product_num: 1,
					spec_code: 0,
					jyshop_pinorder_id:0,//开团主订单id
				},
				
				cardparam: {},
				cfrom: "common" //来源 commonbuy,pintuan,card
			}
		},
		 watch: {
		     express_price:function(newvalue,oldvalue){
				this.changepayprice(); 
			 },
			 coupon_plusamount:function(newvalue,oldvalue){
				 this.changepayprice(); 
			 }
			 
		 },
		onLoad(option) {
			console.log(option);
			this.cfrom = option.cfrom || 'commonbuy';
			
			if(this.cfrom=="pintuan"){
				this.pintuanparam.buy_type =option.buy_type;
				this.pintuanparam.product_id=option.product_id;
				this.pintuanparam.product_num = option.product_num;
				this.pintuanparam.spec_code=option.spec_code;
				this.jyshop_pinorder_id=option.jyshop_pinorder_id;
			}else if(this.cfrom=="commonbuy"){
				this.commonbuyparam.product_id = option.product_id;
				this.commonbuyparam.product_num = option.product_num;
				this.commonbuyparam.spec_code = option.spec_code;
			}	
			//购物车不需要参数读取 token就能获得
			this.getProductList();
		 
		},
		onShow() {
			let that = this;
			uni.$on("selectaddress",res=>{
				that.addressData = res;
				console.log("选择地址后更新订单页数据了");
				uni.$off("selectaddress");
			 
				let prodidarr = [];
				that.product_list.forEach(item=>{
					prodidarr.push(item.id);
				});
				let params={
					address_id:this.addressData.id,
					data:JSON.stringify(prodidarr)
				}
				//重新算运费
				this.$util.req("/express/getKuaidiFei",params,true,function(res){
			 
					 
					that.express_price = res.data.total_price;
					console.log("重新计算运费");
				});
			});
		},
		methods: {
 
 
			//选中的优惠券
			selectcoupon(e){
				this.usedcoupon = e.detail.value;
				//计算优惠金额
				 let discutamount =0;
				this.couponList.forEach(item=>{
					if(this.usedcoupon.includes(item.id)){
						discutamount +=item.coupon_amount;
						console.log(item.coupon_amount);
					}
				});
				this.coupon_plusamount = discutamount;
					console.log(this.coupon_plusamount+","+discutamount);
		 
			
			},
			//获得产品数据
			getProductList() {
				let that = this;
				let params = {
					cfrom: this.cfrom,
					data: ""
				};
				let data = {};
				if (this.cfrom == "commonbuy") {
					data = [{
						'product_id': this.commonbuyparam.product_id,
						'product_num': this.commonbuyparam.product_num,
						'spec_code': this.commonbuyparam.spec_code,
						'price_field': 'sales_price'
					}];
				} else if (this.cfrom == "pintuan") {
					let price_field = this.pintuanparam.buy_type == "notuan" ? 'market_price' : 'sales_price';
					data = {

						'product_id': this.pintuanparam.product_id,
						'product_num': this.pintuanparam.product_num,
						'spec_code': this.pintuanparam.spec_code,
						'price_field': price_field
					};
				 
				 
				}

				params.data = JSON.stringify(data);
				this.$util.req('/card/getProductList', params, true, function(res) {
					that.product_list = res.data.product_list;
					that.total_price = res.data.total_sales_price;
					that.payprice = that.total_price;	
					that.addressData = res.data.address !=null && res.data.address;//地址信息
					that.express_price = res.data.expressamount;
					res.data.couponlist.forEach(item=>{
						item.id=item.id.toString();
					});
					that.couponList = res.data.couponlist;   
					
				
					 
				});
			},
			//当选了优惠券或选了地址后，更改支付价格
			changepayprice(){
				console.log("进入 changepayprice");
				this.payprice = this.total_price-this.coupon_plusamount-this.express_price;
			},
			//显示优惠券面板
			toggleMask(type) {
				if(this.couponList.length==0){
					return;
				}
				let timer = type === 'show' ? 10 : 300;
				let state = type === 'show' ? 1 : 0;
				this.maskState = 2;
				setTimeout(() => {
					this.maskState = state;
				}, timer)
			},
			numberChange(data) {
				this.number = data.number;
			},
			changePayType(type) {
				this.payType = type;
			},
			//创建订单
			submit() {
				if(!this.addressData.id){
					uni.showToast({
						title:"请选择地址"
					})
					return ;
				}
				if(this.cfrom=="pintuan"){
					return this.createPintuanOrder();
				}else{
					return this.createCommonOrder();
				}
				// uni.redirectTo({
				// 	url: '/pages/money/pay'
				// })
			},
			createCommonOrder(){
				let data={
					cfrom:this.cfrom,
					address_id:this.addressData.id,
					jyshop_user_coupon_ids:this.usedcoupon.join(","),
					remark:this.remark,
					product_id:this.commonbuyparam.product_id,
					spec_code:this.commonbuyparam.spec_code,
					product_num:this.commonbuyparam.product_num,
				}
				
							 
				this.$util.req("/order/createOrder",{"data":JSON.stringify(data)},true,function(res){
 
					uni.redirectTo({
						url:"/pages/money/pay?ordertable=jyshop_order&orderid="+res.data.orderid
					});
				});
			},
			createPintuanOrder(){
				let data={
					address_id:this.addressData.id,
					buy_type:this.pintuanparam.buy_type,
					jyshop_user_coupon_ids:this.usedcoupon.join(","),
					product_id:this.pintuanparam.product_id,
					jyshop_pinorder_id:this.pintuanparam.jyshop_pinorder_id,
					spec_code:this.pintuanparam.spec_code,
					product_num:this.pintuanparam.product_num,
					remark:this.remark
				}
			 
				this.$util.req("/orderpin/createOrder",{"data":JSON.stringify(data)},true,function(res){
					uni.redirectTo({
						url:"/pages/money/pay?ordertable=jyshop_pinorder_product&id="+res.data.orderid
					});
				});
			},
			stopPrevent() {}
		}
	}
</script>

<style lang="scss">
	page {
		background: $page-color-base;
		padding-bottom: 100upx;
	}

	.address-section {
		padding: 30upx 0;
		background: #fff;
		position: relative;
		border-bottom: 2px solid $border-color-base;
		.order-content {
			display: flex;
			align-items: center;
			
		}

		.icon-shouhuodizhi {
			flex-shrink: 0;
			display: flex;
			align-items: center;
			justify-content: center;
			width: 90upx;
			color: #888;
			font-size: 44upx;
		}

		.cen {
			display: flex;
			flex-direction: column;
			flex: 1;
			font-size: 28upx;
			color: $font-color-dark;
		}

		.name {
			font-size: 34upx;
			margin-right: 24upx;
		}

		.address {
			margin-top: 16upx;
			margin-right: 20upx;
			color: $font-color-light;
		}

		.icon-you {
			font-size: 32upx;
			color: $font-color-light;
			margin-right: 30upx;
		}

		.a-bg {
			position: absolute;
			left: 0;
			bottom: 0;
			display: block;
			width: 100%;
			height: 5upx;
		}
	}

	.goods-section {
		margin-top: 16upx;
		background: #fff;
		padding-bottom: 1px;

		.g-header {
			display: flex;
			align-items: center;
			height: 84upx;
			// padding: 0 30upx;
			position: relative;
		}

		.logo {
			display: block;
			width: 50upx;
			height: 50upx;
			border-radius: 100px;
		}

		.name {
			font-size: 30upx;
			color: $font-color-base;
			margin-left: 24upx;
		}

		.g-item {
			display: flex;
			margin: 20upx 30upx;

			image {
				flex-shrink: 0;
				display: block;
				width: 140upx;
				height: 140upx;
				border-radius: 4upx;
			}

			.right {
				flex: 1;
				padding-left: 24upx;
				overflow: hidden;
			}

			.title {
				font-size: 30upx;
				color: $font-color-dark;
			}

			.spec {
				font-size: 26upx;
				color: $font-color-light;
			}

			.price-box {
				display: flex;
				align-items: center;
				font-size: 32upx;
				color: $font-color-dark;
				padding-top: 10upx;

				.price {
					margin-bottom: 4upx;
				}

				.number {
					font-size: 26upx;
					color: $font-color-base;
					margin-left: 20upx;
				}
			}

			.step-box {
				position: relative;
			}
		}
	}

	.yt-list {
		margin-top: 16upx;
		background: #fff;
	}

	.yt-list-cell {
		display: flex;
		align-items: center;
		padding: 10upx 30upx 10upx 40upx;
		line-height: 70upx;
		position: relative;

		&.cell-hover {
			background: #fafafa;
		}

		&.b-b:after {
			left: 30upx;
		}

		.cell-icon {
			height: 32upx;
			width: 32upx;
			font-size: 22upx;
			color: #fff;
			text-align: center;
			line-height: 32upx;
			background: #f85e52;
			border-radius: 4upx;
			margin-right: 12upx;

			&.hb {
				background: #ffaa0e;
			}

			&.lpk {
				background: #3ab54a;
			}

		}

		.cell-more {
			align-self: center;
			font-size: 24upx;
			color: $font-color-light;
			margin-left: 8upx;
			margin-right: -10upx;
		}

		.cell-tit {
			flex: 1;
			font-size: 26upx;
			color: $font-color-light;
			margin-right: 10upx;
		}

		.cell-tip {
			font-size: 26upx;
			color: $font-color-dark;

			&.disabled {
				color: $font-color-light;
			}

			&.active {
				color: $base-color;
			}

			&.red {
				color: $base-color;
			}
		}

		&.desc-cell {
			.cell-tit {
				max-width: 90upx;
			}
		}

		.desc {
			flex: 1;
			font-size: $font-base;
			color: $font-color-dark;
		}
	}

	/* 支付列表 */
	.pay-list {
		padding-left: 40upx;
		margin-top: 16upx;
		background: #fff;

		.pay-item {
			display: flex;
			align-items: center;
			padding-right: 20upx;
			line-height: 1;
			height: 110upx;
			position: relative;
		}

		.icon-weixinzhifu {
			width: 80upx;
			font-size: 40upx;
			color: #6BCC03;
		}

		.icon-alipay {
			width: 80upx;
			font-size: 40upx;
			color: #06B4FD;
		}

		.icon-xuanzhong2 {
			display: flex;
			align-items: center;
			justify-content: center;
			width: 60upx;
			height: 60upx;
			font-size: 40upx;
			color: $base-color;
		}

		.tit {
			font-size: 32upx;
			color: $font-color-dark;
			flex: 1;
		}
	}

	.footer {
		position: fixed;
		left: 0;
		bottom: 0;
		z-index: 995;
		display: flex;
		align-items: center;
		width: 100%;
		height: 90upx;
		justify-content: space-between;
		font-size: 30upx;
		background-color: #fff;
		z-index: 998;
		color: $font-color-base;
		box-shadow: 0 -1px 5px rgba(0, 0, 0, .1);

		.price-content {
			padding-left: 30upx;
		}

		.price-tip {
			color: $base-color;
			margin-left: 8upx;
		}

		.price {
			font-size: 36upx;
			color: $base-color;
		}

		.submit {
			display: flex;
			align-items: center;
			justify-content: center;
			width: 280upx;
			height: 100%;
			color: #fff;
			font-size: 32upx;
			background-color: $base-color;
		}
	}

	/* 优惠券面板 */
	.mask {
		display: flex;
		align-items: flex-end;
		position: fixed;
		left: 0;
		top: var(--window-top);
		bottom: 0;
		width: 100%;
		background: rgba(0, 0, 0, 0);
		z-index: 9995;
		transition: .3s;

		.mask-content {
			width: 100%;
			min-height: 30vh;
			max-height: 70vh;
			background: #f3f3f3;
			transform: translateY(100%);
			transition: .3s;
			overflow-y: scroll;
		}

		&.none {
			display: none;
		}

		&.show {
			background: rgba(0, 0, 0, .4);

			.mask-content {
				transform: translateY(0);
			}
		}
	}
	/* 优惠券列表 */
	.coupon-item {
		display: flex;
		flex-direction: row;
		margin: 20upx 10upx;
		padding: 10upx;
		height: 150upx;
		background: #fff;
		border-radius: 10upx;
		.markitget{
			position: absolute;
			right: 10upx;	
			padding: 10upx;
			transform: rotate(30deg);
			font-size: $font-sm;
		}
        .item-left{
			padding: 20upx 20rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			flex-direction: column;
			flex:1;
			font-size: 48upx;
			background-color:#ffa200 ;
			color:#FFFFFF;
			.tip{
				font-size: $font-sm;
			}
		}
		.item-right{
			width: 100%;
			padding-left: 10upx;
			flex:2;
			 color:$font-color-base;
			font-size: $font-base;
			display: flex;
			flex-direction: column;
			justify-content: space-between;
			.desc{
				display: flex;
				flex-direction: row;
				align-items: center;
				justify-content: space-between;
				.getbtn{
 					color:#FF0000;
		 
				 
				}
			}
		}
		
	 
		 

	 

		 
 
	}
	 
</style>
